/*
 * Created by JFormDesigner on Fri Jun 28 14:50:05 CST 2024
 */

package scct.yangxianpeng.ui;

import java.awt.event.*;

import scct.yangxianpeng.dao.TypeDao;
import scct.yangxianpeng.model.GoodsType;

import java.awt.*;
import java.util.List;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;

/**
 * @author 35403
 */
public class PanelType extends JPanel {
    FormMain main;
    DefaultTableModel model;

    public PanelType(FormMain main) {
        initComponents();
        this.main = main;
        initView();
    }

    private void initView() {
        model = (DefaultTableModel) tabType.getModel();
        model.addColumn("id");
        model.addColumn("类别名称");
        model.addColumn("类别说明");
        getTable();
    }

    private void getTable() {
        model.setRowCount(0);
        TypeDao typeDao = new TypeDao();
        List<GoodsType> allTyps = typeDao.getAllTyps();
        for (GoodsType allTyp : allTyps) {
            model.addRow(new Object[]{allTyp.getId(), allTyp.getName(), allTyp.getRemark()});
        }
        model.fireTableDataChanged();
    }

    //    刷新
    //以上代码创建每个页面有要写
    private void btnAdd(ActionEvent e) {
        DlgAddType dlgAddType = new DlgAddType(main);
        dlgAddType.setModal(true);
        dlgAddType.setVisible(true);
        getTable();
    }

    //    修改商品类别
    private void btnUpdate(ActionEvent e) {
        //        判断用户是否选中表中的一行
        if (tabType.getSelectedRowCount() == 0) {
            JOptionPane.showMessageDialog(this, "请选择要修改的商品类别！");
            return;
        }
        int index = tabType.getSelectedRow();
        int id = Integer.parseInt(model.getValueAt(index, 0).toString());
        DlgTypeUpdate dlgTypeUpdate = new DlgTypeUpdate(main, id);
        dlgTypeUpdate.setModal(true);
        dlgTypeUpdate.setVisible(true);
        getTable();
    }

    private void initComponents() {
//         JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
// Generated using JFormDesigner Evaluation license - yang
panel1 = new JPanel();
btnAdd = new JButton();
btnUpdate = new JButton();
scrollPane1 = new JScrollPane();
tabType = new JTable();

//======== this ========
setBorder ( new javax . swing. border .CompoundBorder ( new javax . swing. border .TitledBorder ( new javax .
swing. border .EmptyBorder ( 0, 0 ,0 , 0) ,  "JFor\u006dDesi\u0067ner \u0045valu\u0061tion" , javax. swing .border
. TitledBorder. CENTER ,javax . swing. border .TitledBorder . BOTTOM, new java. awt .Font ( "Dia\u006cog"
, java .awt . Font. BOLD ,12 ) ,java . awt. Color .red ) , getBorder
() ) );  addPropertyChangeListener( new java. beans .PropertyChangeListener ( ){ @Override public void propertyChange (java
. beans. PropertyChangeEvent e) { if( "bord\u0065r" .equals ( e. getPropertyName () ) )throw new RuntimeException
( ) ;} } );
setLayout(new BorderLayout());

//======== panel1 ========
{
    panel1.setLayout(new FlowLayout(FlowLayout.RIGHT));

    //---- btnAdd ----
    btnAdd.setText("\u65b0\u589e\u7c7b\u522b");
    btnAdd.addActionListener(e -> btnAdd(e));
    panel1.add(btnAdd);

    //---- btnUpdate ----
    btnUpdate.setText("\u4fee\u6539\u7c7b\u522b");
    btnUpdate.addActionListener(e -> btnUpdate(e));
    panel1.add(btnUpdate);
}
add(panel1, BorderLayout.NORTH);

//======== scrollPane1 ========
{
    scrollPane1.setViewportView(tabType);
}
add(scrollPane1, BorderLayout.CENTER);
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    // Generated using JFormDesigner Evaluation license - yang
    private JPanel panel1;
    private JButton btnAdd;
    private JButton btnUpdate;
    private JScrollPane scrollPane1;
    private JTable tabType;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
